package com.wfl.sampling.web;

import com.wfl.sampling.anno.Log;
import com.wfl.sampling.aop.NoAuth;
import com.wfl.sampling.entity.PostClick;
import com.wfl.sampling.entity.PostDo;
import com.wfl.sampling.entity.PostNew;
import com.wfl.sampling.entity.Result;
import com.wfl.sampling.service.LoadFileService;
import com.wfl.sampling.service.PostService;
import com.wfl.sampling.utils.AIiOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RestController
@RequestMapping("/api")
public class PostController {
    @Autowired
    private PostService postService;
    @Autowired
    private LoadFileService loadFileService;
    @Log
    @NoAuth
    @PostMapping("/create")
    public ResponseEntity<?> createPost(@RequestBody PostDo post) {
        postService.createPost(post);
        return ResponseEntity.ok("Post created successfully.");
    }

    @NoAuth
    @GetMapping("/find")
    public ResponseEntity<?> getPostById() {
        List<PostDo> post = postService.findPost();
        if (post == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(post);
    }
    @Log
    @DeleteMapping("/{postId}")
    public void deletePostById(@PathVariable Integer postId) {
        postService.deletePost(postId);
    }


    @NoAuth
    @Log
    @PostMapping("/load")
    public List<String> getImageFiles(List<MultipartFile> imageFiles){
        List<String> imagePaths = loadFileService.loadFiles(imageFiles);
        return imagePaths;
    }

    @NoAuth
    @Log
    @PostMapping("/loadAvatar")
    public Result newGetImageFiles(MultipartFile imageFile){
        String imagePath = loadFileService.newLoadFiles(imageFile);
        if(imagePath==null)
            return Result.error("头像违规或上传失败！");
        return Result.success(imagePath);
    }

    @NoAuth
    @Log
    @PostMapping("/postNew")
    public Result sendComment(@RequestBody PostNew postNew){
        if(postService.insertComment(postNew))
            return Result.success("评论成功！");
        else
            return Result.error("评论失败");
    }



    //点赞
    @PostMapping("/postClick")
    public Result commitClick(@RequestBody PostClick postClick){
        if(postService.insertClick(postClick))
            return Result.success("操作成功");
        else
            return Result.error("操作失败");
    }

    @NoAuth
    @GetMapping("/postIdClick")
    public Result allClick(@RequestParam("id") Integer id){
        List<Integer> postIds = postService.getAllClick(id);
        if(postIds==null)
            return Result.success(null);
        else
            return Result.success(postIds);

    }



//    @PostMapping("/load")
//    public CompletableFuture<List<String>> getImageFiles(@RequestParam("imageFiles") List<MultipartFile> imageFiles) {
//        return CompletableFuture.supplyAsync(() -> loadFileService.loadFiles(imageFiles));
//    }

}
